//
//  XLEdNickViewController.m
//  XiangLove
//
//  Created by feng song on 2017/12/5.
//  Copyright © 2017年 BAOMIHUA.COM. All rights reserved.
//

#import "XLEdNickViewController.h"
#import "NSString+Emoji.h"
#import "XLHTTPSessionManager+UserInfo.h"
#import <ReactiveCocoa.h>
#import "XLAccountManager.h"
#import "XLUserInfoModel.h"



#define MAX_STARWORDS_LENGTH 10

@interface XLEdNickViewController ()<UITextFieldDelegate>
@property (nonatomic,strong)XLUserInfoModel    *userInfoModel;

@end

@implementation XLEdNickViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"昵称";
    
    [self showBackButton:NULL];
    self.saveNickStr = [[NSString alloc] init];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(finishActionbar)];
    
    [self makeUI];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldEditChanged:)
                                                name:@"UITextFieldTextDidChangeNotification" object:self.textField];
    
    
    
    
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleDefault;
}


-(void)makeUI
{
    _textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10,XQScreenW-20,60)];
    _textField.text = self.nickStr;
    _textField.delegate = self;
    _textField.textColor = [UIColor blackColor];
    _textField.backgroundColor = XLThemeColor;
    _textField.font = [UIFont systemFontOfSize:14.0];
    _textField.borderStyle = UITextBorderStyleRoundedRect;
    UIButton *codeClean = [_textField valueForKey:@"_clearButton"];
    [codeClean setImage:[UIImage imageNamed:@"me_content_but_n"] forState:UIControlStateNormal];
    _textField.clearButtonMode = UITextFieldViewModeAlways;
    _textField.keyboardAppearance = UIKeyboardAppearanceLight;
    [self.view addSubview:_textField];
    self.saveNickStr = self.nickStr;
}




- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSLog(@"location-->>%lu",(unsigned long)range.location);
    NSLog(@"replacementString-->>%@",string);
    //禁止输入emoji表情
        if ([NSString stringContainsEmoji:string]) {

            [MBProgressHUD showContent:@"暂不支持表情功能!"];
            return NO;
        }
    
    
    return YES;
}

-(void)textFieldEditChanged:(NSNotification *)obj
{
    
    UITextField *textField = (UITextField *)obj.object;
    NSString *toBeString = textField.text;
    self.saveNickStr = toBeString;
    
    //获取高亮部分
    UITextRange *selectedRange = [textField markedTextRange];
    UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
    
    // 没有高亮选择的字，则对已输入的文字进行字数统计和限制
    if (!position)
    {
        if (toBeString.length > MAX_STARWORDS_LENGTH)
        {
            NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:MAX_STARWORDS_LENGTH];
            if (rangeIndex.length == 1)
            {
                textField.text = [toBeString substringToIndex:MAX_STARWORDS_LENGTH];
                self.saveNickStr = textField.text;
            }
            else
            {
                NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, MAX_STARWORDS_LENGTH)];
                textField.text = [toBeString substringWithRange:rangeRange];
                self.saveNickStr = textField.text;
            }
        }
    }

    
}



- (void)finishActionbar {
    [self.textField resignFirstResponder];
    
    self.saveNickStr = [self.saveNickStr stringByReplacingOccurrencesOfString:@" " withString:@""];
    if (self.saveNickStr.length == 0) {
        [NSString addMBProgressHUD:@"请完善昵称信息" showHUDToView:self.view];
        return;
    }
    NSLog(@"我的设置的昵称==%@",self.saveNickStr);
    [self setUpdata];
    
    
}

-(void)setUpdata
{
    
    if (self.setBackBlock) {
        
        self.setBackBlock(self.saveNickStr);
    }
    [self.navigationController popViewControllerAnimated:YES];
    
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.textField resignFirstResponder];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
